//
//  YFDatePickerView.m
//  DatePickerDemo
//
//  Created by Winny Zhang on 2019/4/4.
//  Copyright © 2019 Winny Zhang. All rights reserved.
//

#import "YFDatePickerView.h"
#import "YFDatePicker.h"
#import "YFTimePicker.h"
#import "NSDate+YFExtension.h"


@interface YFDatePickerView ()

/**
 背景 View
 */
@property (weak, nonatomic) IBOutlet UIView *contentView;
/**
 年份 Label
 */
@property (weak, nonatomic) IBOutlet UILabel *yearLabel;
/**
 日期 Label
 */
@property (weak, nonatomic) IBOutlet UILabel *datelabel;

/**
 时间 Label
 */
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;


/**
 日期选择器背景View
 */
@property (weak, nonatomic) IBOutlet UIView *dateView;
/**
 时间选择器背景View
 */
@property (weak, nonatomic) IBOutlet UIView *timeView;


/**
 日期选择器
 */
@property (nonatomic, strong) YFDatePicker *datePicker;

/**
 时间选择器
 */
@property (nonatomic,strong) YFTimePicker *timerPicker;

@property (weak, nonatomic) IBOutlet UIButton *okBtn;
#pragma mark - 记录当前时间
/**
 年
 */
@property (nonatomic, copy) NSString *year;
/**
 月
 */
@property (nonatomic, copy) NSString *month;
/**
 日
 */
@property (nonatomic, copy) NSString *day;
/**
 时
 */
@property (nonatomic, copy) NSString *hour;
/**
 分
 */
@property (nonatomic, copy) NSString *minute;


@end

@implementation YFDatePickerView

+ (instancetype)datePickerView {
    YFDatePickerView *view = [[NSBundle mainBundle] loadNibNamed:@"YFDatePickerView" owner:nil options:nil].firstObject;
    view.frame = [UIScreen mainScreen].bounds;
    return view;
}

- (void)show {
    self.alpha = 0;
    self.contentView.transform = CGAffineTransformMakeScale(0, 0);
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:1 options:(UIViewAnimationOptionCurveEaseOut) animations:^{
        self.alpha = 1;
        self.contentView.transform = CGAffineTransformMakeScale(1.7, 1.7);
        self.contentView.transform = CGAffineTransformMakeScale(1, 1);
    } completion:^(BOOL finished) {
        self.contentView.transform = CGAffineTransformIdentity;
    }];
}


- (void)dismiss {
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:1 options:(UIViewAnimationOptionCurveEaseOut) animations:^{
        self.alpha = 0;
        self.contentView.transform = CGAffineTransformMakeScale(0, 0);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        self.alpha = 1;
        self.contentView.transform = CGAffineTransformIdentity;
    }];
}


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupViews];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setupViews];
    }
    return  self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setupViews];
}

- (void)setupViews {
    
    self.contentView.layer.cornerRadius = 6;
    self.contentView.layer.masksToBounds = YES;
    
    self.okBtn.layer.cornerRadius = 6;
    self.okBtn.layer.masksToBounds = YES;
    
    self.yearLabel.text = [NSString stringWithFormat:@"%zd", [NSDate date].currentYear];
    self.datelabel.text = [NSString stringWithFormat:@"%zd月%zd日", [NSDate date].currentMonth, [NSDate date].currentDay];
    self.timeLabel.text = [NSString stringWithFormat:@"%zd:%zd", [NSDate date].currentHour, [NSDate date].currentMinute];
    
    // 赋初值
    self.year = [NSString stringWithFormat:@"%zd", [NSDate date].currentYear];
    self.month = [NSString stringWithFormat:@"%zd", [NSDate date].currentMonth];
    self.day = [NSString stringWithFormat:@"%zd", [NSDate date].currentDay];
    self.hour = [NSString stringWithFormat:@"%zd", [NSDate date].currentHour];
    self.minute = [NSString stringWithFormat:@"%zd", [NSDate date].currentMinute];
    
    // 日期选择器
    YFDatePicker *datePicker = [YFDatePicker datePickerWithFrame:CGRectMake(30, 0, self.frame.size.width - 120, 120)];
    datePicker.minimumYear = 1970;
    datePicker.maximumYear = 2050;
    [self.dateView addSubview:datePicker];
    self.datePicker = datePicker;
    
    __weak typeof(self) weakSelf = self;
    
    self.datePicker.selectedDateCompletion = ^(NSString * _Nonnull year, NSString * _Nonnull month, NSString * _Nonnull day) {
        
        weakSelf.yearLabel.text = year;
        weakSelf.datelabel.text = [NSString stringWithFormat:@"%@月%@日", month, day];
        
        weakSelf.year = year;
        weakSelf.month = month;
        weakSelf.day = day;
    };
    
    // 时间选择器
    YFTimePicker *timerPicker = [YFTimePicker timePickerWithFrame:CGRectMake(60, 0, self.frame.size.width - 180, 120)];
    [self.timeView addSubview:timerPicker];
    self.timerPicker = timerPicker;
    self.timerPicker.selectedTimeCompletion = ^(NSString * _Nonnull hour, NSString * _Nonnull minute) {
        
        weakSelf.timeLabel.text = [NSString stringWithFormat:@"%@:%@", hour, minute];
        weakSelf.hour = hour;
        weakSelf.minute = minute;
    };
    
    // 设置点击背景消失
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
    [self.contentView addGestureRecognizer:tap];
}

#pragma mark - 按钮点击事件

- (IBAction)dateButtonClicked:(id)sender {
   
    if (_selectCompleted) {
        _selectCompleted(_year, _month, _day, _hour, _minute);
    }
    [self dismiss];
}




@end
